package puzzle.game.states;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.state.transition.Transition;
import puzzle.game.BallPuzzle;

public abstract class PuzzleState extends BasicGameState {
  
  protected BallPuzzle context;
  private boolean started=false;
  
  public static final Color TRANSPARENT = new Color(0f, 0f, 0f, 0f);
  private static Color currentBG = Color.black;
  private Color bg = Color.black;
  
  public PuzzleState(BallPuzzle ctx) {
    this.context = ctx;
  }
  
  public BallPuzzle getContext() {
    return context;
  }
  
  public final void init(GameContainer container, StateBasedGame game) throws SlickException {
    initState(container);
  }
  
  public final void render(GameContainer container, StateBasedGame game, Graphics g) {
    if (currentBG!=getBackground()) {
      g.setBackground(getBackground()); 
      currentBG = getBackground();
    }
    renderState(container, g);
  }
  
  public final void update(GameContainer container, StateBasedGame game, int delta) {
    updateState(container, delta);
  }
  
  public boolean isStarted() {
    return started;
  }
  
  public void setStarted(boolean t) {
    started = t;
  }
  
  public void setBackground(Color c) {
    bg = c;
  }
  
  public Color getBackground() {
    return bg;
  }
  
  public abstract void initState(GameContainer c) throws SlickException;
  public abstract void renderState(GameContainer c, Graphics graphics);
  public abstract void updateState(GameContainer c, int delta);
  
  public boolean isKeyDown(Input in, int[] controls) {
    for (int i : controls) {
      if (in.isKeyDown(i))
        return true;  
    }
    return false;
  }
  
  public Transition getExitTransition() {
    return new FadeOutTransition(Color.white);
  }
  
  public Transition getEnterTransition() {
    return new FadeInTransition(Color.white);
  }
  
  public void startState(GameContainer container) throws SlickException {
  }
  
  public void keyPressed(int key, char c) {
    getContext().globalKeyPressed(key, c);
  }
}
